home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / Macintosh Drag and Drop / Demo Applications / DragsAndLists / Application.c next >
Encoding:
C/C++ Source or Header  |  1995-04-04  |  3.3 KB  |  187 lines  |  [TEXT/MPS ]

  1. #include <QuickDraw.h>
  2. #include <Dialogs.h>
  3. #include <Fonts.h>
  4. #include <Processes.h>
  5. #include <TextEdit.h>
  6. #include <Events.h>
  7. #include <Menus.h>
  8. #include <Memory.h>
  9. #include <Errors.h>
  10. #include <ToolUtils.h>
  11.  
  12. #ifdef __powerc
  13. QDGlobals qd;
  14. #endif
  15.  
  16.  
  17.  
  18. #include "ListCode.h"
  19.  
  20. void InitApplication(void);
  21. void CreateNewWindow(void);
  22. void MainEventLoop(void);
  23. void MenuCommand(long whaHappened);
  24. void DoAboutBox(void);
  25.  
  26. void PreEventLoop(void);
  27. void PostEventLoop(void);
  28. pascal void DrawWindowContent(short, short, GDHandle, long);
  29. void DrawIt(WindowPtr win);
  30. void DoUpdate(WindowPtr thisWindow);
  31.  
  32. static Boolean gDone;
  33.  
  34.  
  35. /*------------------------------------------------------------------*/
  36.  
  37. void main()
  38. {
  39.  
  40.     InitApplication();
  41.     PreEventLoop();
  42.     MainEventLoop();
  43. }
  44.  
  45.  
  46. /*------------------------------------------------------------------*/
  47.  
  48. void InitApplication()
  49. {
  50.     Handle theMenu;
  51.  
  52.     // Toolbox initialization
  53.     MaxApplZone();
  54.     InitGraf(&qd.thePort);
  55.     InitFonts();
  56.     InitWindows();
  57.     InitMenus();
  58.     TEInit();
  59.     InitDialogs(nil);
  60.     InitCursor();
  61.     FlushEvents(0,everyEvent);
  62.     
  63.     // Application initialization
  64.     gDone = false;
  65.     
  66.     theMenu = GetNewMBar(128);
  67.     if ( theMenu == nil )
  68.         goto MenuStuffFailed;
  69.  
  70.     SetMenuBar(theMenu);
  71.     AddResMenu(GetMHandle(128), 'DRVR');
  72.     DrawMenuBar();
  73.  
  74.     return;
  75.     
  76. MenuStuffFailed:
  77.     // If the menu stuff failed, something just ain't right (most likely some 
  78.     // resources are missing.
  79.     gDone = true;
  80.     return;
  81.  
  82. }
  83.  
  84.  
  85. /*------------------------------------------------------------------*/
  86.  
  87. void DoAboutBox()
  88. {
  89.     (void) Alert(128, nil);
  90. }
  91.  
  92.  
  93. /*------------------------------------------------------------------*/
  94.  
  95. void MainEventLoop()
  96. {
  97.     EventRecord        theEvent;
  98.     WindowPtr        thisWindow;
  99.     short            clickArea;
  100.     Rect            screenRect;
  101.     long            menuResult;
  102.     char            charCode;
  103.  
  104.     while ( !gDone )
  105.     {
  106.         if ( WaitNextEvent(everyEvent, &theEvent, 0, nil) )
  107.         {
  108.             switch (theEvent.what)
  109.             {
  110.                 case mouseDown:
  111.                     clickArea = FindWindow(theEvent.where, &thisWindow);
  112.                     
  113.                     if (clickArea == inDrag)
  114.                     {
  115.                         screenRect = (**GetGrayRgn()).rgnBBox;
  116.                         DragWindow(thisWindow, theEvent.where, &screenRect);
  117.                     }
  118.                     else if ( clickArea == inContent )
  119.                     {
  120.                         if ( thisWindow != FrontWindow() )
  121.                             SelectWindow(thisWindow);
  122.                         else
  123.                             (void) HandleListClick(thisWindow, &theEvent);
  124.  
  125.                     }
  126.                     else if (clickArea == inGoAway)
  127.                     {
  128.                         if ( TrackGoAway(thisWindow, theEvent.where) )
  129.                             gDone = true;
  130.                     }
  131.                     else if ( clickArea == inMenuBar ) 
  132.                     {
  133.                         menuResult = MenuSelect(theEvent.where);
  134.                         if ( (menuResult  >> 16) != 0 )
  135.                         {
  136.                             MenuCommand(menuResult);
  137.                             HiliteMenu(0);
  138.                         }
  139.                     }
  140.                     break;
  141.                 case keyDown:
  142.                     charCode = theEvent.message & charCodeMask;
  143.  
  144.                     if ( (theEvent.modifiers & cmdKey) != 0 ) 
  145.                     {    
  146.                         menuResult = MenuKey(charCode);
  147.                 
  148.                         if ( (menuResult  >> 16) != 0 )
  149.                             MenuCommand(menuResult);
  150.                             
  151.                     } 
  152.                     break;
  153.                 case updateEvt:
  154.                     thisWindow = (WindowPtr)theEvent.message;    
  155.                     DoUpdate(thisWindow);
  156.                 
  157.                     break;
  158.                 
  159.             }
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165.  
  166. /*------------------------------------------------------------------*/
  167.  
  168. void MenuCommand(long whaHappened)
  169. {
  170.     short    menuID, menuItem;
  171.     
  172.     menuID = (whaHappened >> 16);
  173.     menuItem = (whaHappened & 0xFFFF);
  174.     
  175.     if ( menuID == 128 )
  176.     {
  177.         if ( menuItem == 1)
  178.             DoAboutBox();
  179.     }
  180.     else if ( menuID == 129 )
  181.     {
  182.         if (menuItem == 1)
  183.             gDone = true;
  184.     }
  185. }
  186.  
  187.